﻿using System;
using System.Data;
using System.Text;
using PSAMMatrixCloud.Model;

namespace PSAMMatrixCloud.DAL 
{
	public class psam_matrixDal
	{
		public psam_matrixDal()
		{}

		#region  Basic


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string id)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) from psam_matrix");
			strSql.Append(" where id = '" + id +"' ");
			return MySqlDB.Exists(MySqlDB.MainDBConfig, strSql.ToString());
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(psam_matrixModel model)
		{
			StringBuilder strSql = new StringBuilder();
			StringBuilder strSql1 = new StringBuilder();
			StringBuilder strSql2 = new StringBuilder();
			if (model.matrixno != null)
			{
				strSql1.Append("matrixno,");
				strSql2.Append("'" + model.matrixno + "',");
			}
			if (model.id != null)
			{
				strSql1.Append("id,");
				strSql2.Append("'" + model.id + "',");
			}
			if (model.matrixip != null)
			{
				strSql1.Append("matrixip,");
				strSql2.Append("'" + model.matrixip + "',");
			}
			if (model.matrixport != null)
			{
				strSql1.Append("matrixport,");
				strSql2.Append("'" + model.matrixport + "',");
			}
			if (model.status != null)
			{
				strSql1.Append("status,");
				strSql2.Append("'" + model.status + "',");
			}
			if (model.maxconnectnum != null)
			{
				strSql1.Append("maxconnectnum,");
				strSql2.Append("'" + model.maxconnectnum + "',");
			}
			if (model.createtime != null)
			{
				strSql1.Append("createtime,");
				strSql2.Append("'" + model.createtime + "',");
			}
			if (model.modifytime != null)
			{
				strSql1.Append("modifytime,");
				strSql2.Append("'" + model.modifytime + "',");
			}
			if (model.createip != null)
			{
				strSql1.Append("createip,");
				strSql2.Append("'" + model.createip + "',");
			}
			if (model.modifyip != null)
			{
				strSql1.Append("modifyip,");
				strSql2.Append("'" + model.modifyip + "',");
			}
			strSql.Append("insert into psam_matrix(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows = MySqlDB.ExecuteSql(MySqlDB.MainDBConfig, strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(psam_matrixModel model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update psam_matrix set ");
			if (model.matrixno != null)
			{
				strSql.Append("matrixno = '" + model.matrixno + "',");
			}
			else
			{
				strSql.Append("matrixno = null ,");
			}
			if (model.id != null)
			{
				strSql.Append("id = '" + model.id + "',");
			}
			else
			{
				strSql.Append("id = null ,");
			}
			if (model.matrixip != null)
			{
				strSql.Append("matrixip = '" + model.matrixip + "',");
			}
			else
			{
				strSql.Append("matrixip = null ,");
			}
			if (model.matrixport != null)
			{
				strSql.Append("matrixport = '" + model.matrixport + "',");
			}
			else
			{
				strSql.Append("matrixport = null ,");
			}
			if (model.status != null)
			{
				strSql.Append("status = '" + model.status + "',");
			}
			else
			{
				strSql.Append("status = null ,");
			}
			if (model.maxconnectnum != null)
			{
				strSql.Append("maxconnectnum = '" + model.maxconnectnum + "',");
			}
			else
			{
				strSql.Append("maxconnectnum = null ,");
			}
			if (model.createtime != null)
			{
				strSql.Append("createtime = '" + model.createtime + "',");
			}
			else
			{
				strSql.Append("createtime = null ,");
			}
			if (model.modifytime != null)
			{
				strSql.Append("modifytime = '" + model.modifytime + "',");
			}
			else
			{
				strSql.Append("modifytime = null ,");
			}
			if (model.createip != null)
			{
				strSql.Append("createip = '" + model.createip + "',");
			}
			else
			{
				strSql.Append("createip = null ,");
			}
			if (model.modifyip != null)
			{
				strSql.Append("modifyip = '" + model.modifyip + "',");
			}
			else
			{
				strSql.Append("modifyip = null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where id = '" + model.id + "' ");
			int rowsAffected = MySqlDB.ExecuteSql(MySqlDB.MainDBConfig, strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string id)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from psam_matrix ");
			strSql.Append(" where id = '" + id +"' ");
			int rowsAffected = MySqlDB.ExecuteSql(MySqlDB.MainDBConfig, strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string list)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from psam_matrix ");
			strSql.Append(" where id in (" + list + ") ");
	
			int rows = MySqlDB.ExecuteSql(MySqlDB.MainDBConfig, strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public psam_matrixModel GetModel(string id)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select  ");
			strSql.Append(" * ");
			strSql.Append(" from psam_matrix ");
			strSql.Append(" where id = '" + id +"' ");
			psam_matrixModel model = new psam_matrixModel();
			DataSet ds = MySqlDB.Query(MySqlDB.MainDBConfig, strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public psam_matrixModel DataRowToModel(DataRow row)
		{
			psam_matrixModel model = new psam_matrixModel();
			if (row != null)
			{
				if(row["matrixno"] != null && !string.IsNullOrEmpty(row["matrixno"].ToString()))
				{
					model.matrixno = row["matrixno"].ToString();
				}
				if(row["id"] != null && !string.IsNullOrEmpty(row["id"].ToString()))
				{
					model.id = row["id"].ToString();
				}
				if(row["matrixip"] != null && !string.IsNullOrEmpty(row["matrixip"].ToString()))
				{
					model.matrixip = row["matrixip"].ToString();
				}
				if(row["matrixport"] != null && !string.IsNullOrEmpty(row["matrixport"].ToString()))
				{
					model.matrixport = row["matrixport"].ToString();
				}
				if(row["status"] != null && !string.IsNullOrEmpty(row["status"].ToString()))
				{
					model.status = Convert.ToInt32(row["status"].ToString());
				}
				if(row["maxconnectnum"] != null && !string.IsNullOrEmpty(row["maxconnectnum"].ToString()))
				{
					model.maxconnectnum = Convert.ToInt32(row["maxconnectnum"].ToString());
				}
				if(row["createtime"] != null && !string.IsNullOrEmpty(row["createtime"].ToString()))
				{
					model.createtime = Convert.ToDateTime(row["createtime"].ToString());
				}
				if(row["modifytime"] != null && !string.IsNullOrEmpty(row["modifytime"].ToString()))
				{
					model.modifytime = Convert.ToDateTime(row["modifytime"].ToString());
				}
				if(row["createip"] != null && !string.IsNullOrEmpty(row["createip"].ToString()))
				{
					model.createip = row["createip"].ToString();
				}
				if(row["modifyip"] != null && !string.IsNullOrEmpty(row["modifyip"].ToString()))
				{
					model.modifyip = row["modifyip"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM psam_matrix ");
			if(strWhere.Trim() != "")
			{
				strSql.Append(" where " + strWhere);
			}
			return MySqlDB.Query(MySqlDB.MainDBConfig, strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) FROM psam_matrix ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where " + strWhere);
			}
			object obj = MySqlDB.GetSingle(MySqlDB.MainDBConfig, strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int pageSize, int pageNo)
		{
            StringBuilder strSql = new StringBuilder("select * from psam_matrix ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }

            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append(" order by " + orderby);
            }
            strSql.AppendFormat(" limit {0},{1}", pageSize * (pageNo - 1), pageSize);

            return MySqlDB.ExecuteDataSet(MySqlDB.MainDBConfig, strSql.ToString());
        }

		#endregion  Basic

		#region  Extension

		#endregion  Extension
	}
}